<template>
  <div class="pagination-container">
    <el-pagination
      :background="background"
      v-model:current-page.sync="currentPage"
      v-model:page-size="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :pager-count="pagerCount"
      :total="total"
      popper-class="pagination-down"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- hide-on-single-page -->
  </div>
  <!-- <div>{{ page }}</div> -->
</template>

<script setup>
import { scrollTo } from "@/utils/scroll-to";

const props = defineProps({
  total: {
    required: true,
    type: Number,
  },
  page: {
    type: Number,
    default: 1,
  },
  limit: {
    type: Number,
    default: 20,
  },
  pageSizes: {
    type: Array,
    default () {
      return [10, 20, 30, 50];
    },
  },
  // 移动端页码按钮的数量端默认值5
  pagerCount: {
    type: Number,
    default: document.body.clientWidth < 992 ? 5 : 7,
  },
  layout: {
    type: String,
    default: "total, prev, pager, next, sizes",
  },
  background: {
    type: Boolean,
    default: true,
  },
  autoScroll: {
    type: Boolean,
    default: true,
  },
  hidden: {
    type: Boolean,
    default: false,
  },
});

const emit = defineEmits();
const currentPage = computed({
  get () {
    // console.log(props.page, "-----")
    return props.page;
  },
  set (val) {
    // console.log(val, "-----")
    emit("update:page", val);
  },
});
const pageSize = computed({
  get () {
    return props.limit;
  },
  set (val) {
    emit("update:limit", val);
  },
});
function handleSizeChange (val) {
  if (currentPage.value * val > props.total) {
    currentPage.value = 1;
  }
  emit("pagination", { page: currentPage.value, limit: val });
  if (props.autoScroll) {
    scrollTo(0, 800);
  }
}
function handleCurrentChange (val) {
  emit("pagination", { page: val, limit: pageSize.value });
  if (props.autoScroll) {
    scrollTo(0, 800);
  }
}
</script>

<style scoped lang="scss">
.pagination-container {
  background: #fff;
  padding: 32px 16px;
}
.pagination-container.hidden {
  display: none;
}
::v-deep(.el-pagination) {
  .el-pager li:hover {
    color: #5974ff;
  }

  &.is-background .el-pager li {
    background: none;
  }

  .el-select .el-input__wrapper.is-focus {
    box-shadow: 0 0 0 1px #5974ff inset !important;
  }

  &.is-background .el-pager li.is-active {
    background: #5974ff;
    color: #fff;
    border-radius: 6px 6px 6px 6px;
  }

  &.is-background .btn-next,
  &.is-background .btn-prev {
    background: none;
  }

  .el-pagination__sizes {
    .el-select .el-input.is-focus .el-input__wrapper {
      box-shadow: 0 0 0 1px #5974ff inset !important;
    }

    .el-input__wrapper {
      box-shadow: none;
      background: #f5f5f5;
      border-radius: 6px 6px 6px 6px;

      .el-input__inner {
        color: #696969 !important;
      }
    }
  }
}
</style>

<style lang="scss">
.pagination-down {
  .el-select-dropdown__item.selected {
    color: #5974ff;
  }
}
</style>
